package edu.umn.contactviewer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This class displays a selected contact.
 * 
 */
public class ContactSelectedActivity extends Activity {

	Contact currentContact = null;
	public static final int ADD_EDIT_CONTACT = 100;
	ToolbarConfig toolbar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.contact_selected_item);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			currentContact = (Contact) extras
					.getSerializable("selected-contact");
		}

		toolbar = new ToolbarConfig(this, currentContact.getName());

		((TextView) this.findViewById(R.id.nameValue)).setText(currentContact
				.getName());
		((TextView) this.findViewById(R.id.titleValue)).setText(currentContact
				.getTitle());
		((TextView) this.findViewById(R.id.phoneValue)).setText(currentContact
				.getPhone());
		((TextView) this.findViewById(R.id.emailValue)).setText(currentContact
				.getEmail());
		((TextView) this.findViewById(R.id.twitterValue))
				.setText(currentContact.getTwitterId());

		Button b = ((Button) toolbar.getActivity().findViewById(
				R.id.toolbar_add_edit_button));
		b.setText(R.string.editButtonValue);
		b.setVisibility(View.VISIBLE);
		b.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(ContactSelectedActivity.this,
						AddEditContactActivity.class);

				intent.putExtra("selected-contact", currentContact);
				intent.putExtra("isAdd", false);

				startActivityForResult(intent, ADD_EDIT_CONTACT);

			}
		});

		Button deleteButton = ((Button) toolbar.getActivity().findViewById(
				R.id.deleteButton));

		deleteButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				AlertDialog.Builder builder = new AlertDialog.Builder(
						ContactSelectedActivity.this);
				builder.setMessage(
						"Are you sure you want to delete "
								+ currentContact.getName() + "?")
						.setCancelable(false)
						.setPositiveButton("Yes",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {

										ContactsDB.remove(currentContact,
												ContactSelectedActivity.this);

										Toast.makeText(
												ContactSelectedActivity.this,
												currentContact.getName()
														+ " has been deleted.",
												Toast.LENGTH_LONG).show();
										
										ContactSelectedActivity.this.finish();
									}
								})
						.setNegativeButton("No",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int id) {
										dialog.cancel();
									}
								});
				AlertDialog alert = builder.create();
				alert.show();

			}
		});

		Intent intent = new Intent();
		toolbar.getActivity().setResult(ContactListActivity.SHOW_CONTACT,
				intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent returnedIntent) {
		super.onActivityResult(requestCode, resultCode, returnedIntent);

		if (ADD_EDIT_CONTACT == resultCode) {
			Bundle extras = returnedIntent.getExtras();
			if (extras != null) {
				currentContact = (Contact) extras
						.getSerializable("selected-contact");
			}

			((TextView) this.findViewById(R.id.nameValue))
					.setText(currentContact.getName());
			((TextView) this.findViewById(R.id.titleValue))
					.setText(currentContact.getTitle());
			((TextView) this.findViewById(R.id.phoneValue))
					.setText(currentContact.getPhone());
			((TextView) this.findViewById(R.id.emailValue))
					.setText(currentContact.getEmail());
			((TextView) this.findViewById(R.id.twitterValue))
					.setText(currentContact.getTwitterId());

			Intent intent = new Intent();
			intent.putExtra("edited-contact", currentContact);

			toolbar.getActivity().setResult(ContactListActivity.SHOW_CONTACT,
					intent);
		}

	}

}
